package com.itheima.service.impl;

import com.itheima.bean.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public List<User> findByUsername(String username) throws IOException {
        //1 加载mybatis的核心配置文件MyBatisConfig.xml
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //2 创建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //4 获取UserMapper对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //5 执行查询操作
//        List<User> list = userMapper.findByUsername("%"+username+"%");
        List<User> list = userMapper.findByUsername(username);
        //6 释放资源,返回结果
        sqlSession.close();
        return list;
    }
}
